package com.gbiac.jeecgRPT;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.DynamicDBUtil;
import org.jeecgframework.core.util.PasswordUtil;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**
 * birt报表controller
 * @author Administrator
 */
@Scope("prototype")
@Controller
@RequestMapping("/reportController")
public class ReportController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReportController.class);
	/**人力资源报表数据源*/
	private static final String DBKEY_employeeSalary = "employee-salary";
	/**查询所有公司的sql*/
	private static final String companySql="select d.organization_id, d.chinese_name from um_organization d where d.deleted = 0 and d.category = 1 and d.is_expired = 0";
	/**查询薪水册的sql*/
	private static final String salaryBookSql = "select book_id,book_name,(select count(1) from hr_salary_item_order_detail  where order_version_id in (select distinct order_version_id from hr_salary_book where book_id=a.book_id and item_type=0)) as col_num from hr_salary_book a where a.deleted = 0  and a.state_id = 2  and a.organization_id = ? and a.book_year = ? and a.book_month = ?";
	
	@Autowired
	private SystemService systemService;
	/**
	 * 工资部门汇总报表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "departSalary")
	public ModelAndView departSalary(HttpServletRequest request) {
		//获取当前登录用户信息
		TSUser user = (TSUser) request.getSession().getAttribute(ResourceUtil.LOCAL_CLINET_USER);
		String sql = companySql;
		//当前用户所在单位不是股份公司，则只能查询当前用户所在单位
		String t = "bi_cd";
		String a = PasswordUtil.encrypt(t, t, PasswordUtil.getStaticSalt());
		if(user.getCurrentDepart().getTSPDepart() != null){
			sql+=" and d.organization_id = "+user.getCurrentDepart().getId();
		}
		//查询所有公司
		List<Map<String,Object>> companyList =  DynamicDBUtil.findList(DBKEY_employeeSalary, sql);
		request.setAttribute("companyList", companyList);
		request.setAttribute("reportName", "depart_sum_salary");
		return new ModelAndView("com/gbiac/jeecgRPT/departSalary");
	}
	/**
	 * 工资明细报表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "salaryDetail")
	public ModelAndView salaryDetail(HttpServletRequest request) {
		//获取当前登录用户信息
		TSUser user = (TSUser) request.getSession().getAttribute(ResourceUtil.LOCAL_CLINET_USER);
		String sql = companySql;
		//当前用户所在单位不是股份公司，则只能查询当前用户所在单位
		if(user.getCurrentDepart().getTSPDepart() != null){
			sql+=" and d.organization_id = "+user.getCurrentDepart().getId();
		}
		//查询所有公司
		List<Map<String,Object>> companyList =  DynamicDBUtil.findList(DBKEY_employeeSalary, sql);
		request.setAttribute("companyList", companyList);
		request.setAttribute("reportName", "salary_detail");
		return new ModelAndView("com/gbiac/jeecgRPT/salaryDetail");
	}
	/**
	 * 工资明细报表(二级人员管理部) 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "secondDepartSalaryDetail")
	public ModelAndView secondDepartSalaryDetail(HttpServletRequest request) {
		//查询所有公司
		List<Map<String,Object>> companyList =  DynamicDBUtil.findList(DBKEY_employeeSalary, companySql);
		request.setAttribute("companyList", companyList);
		request.setAttribute("reportName", "second_depart_salary_detail");
		return new ModelAndView("com/gbiac/jeecgRPT/secondDepartSalaryDetail");
	}
	/**
	 * 工资明细报表(二级人员管理部-员工发放方式) 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "secondDepartEmployeeSalaryDetail")
	public ModelAndView secondDepartEmployeeSalaryDetail(HttpServletRequest request) {
		//查询所有公司
		List<Map<String,Object>> companyList =  DynamicDBUtil.findList(DBKEY_employeeSalary, companySql);
		request.setAttribute("companyList", companyList);
		request.setAttribute("reportName", "second_depart_employee_salary_detail");
		return new ModelAndView("com/gbiac/jeecgRPT/secondDepartEmployeeSalaryDetail");
	}
	/**
	 * 工资明细报表(员工发放方式) 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "employeeSalaryDetail")
	public ModelAndView employeeSalaryDetail(HttpServletRequest request) {
		//获取当前登录用户信息
		TSUser user = (TSUser) request.getSession().getAttribute(ResourceUtil.LOCAL_CLINET_USER);
		String sql = companySql;
		//当前用户所在单位不是股份公司，则只能查询当前用户所在单位
		if(user.getCurrentDepart().getTSPDepart() != null){
			sql+=" and d.organization_id = "+user.getCurrentDepart().getId();
		}
		//查询所有公司
		List<Map<String,Object>> companyList =  DynamicDBUtil.findList(DBKEY_employeeSalary, sql);
		request.setAttribute("companyList", companyList);
		request.setAttribute("reportName", "employee_salary_detail");
		return new ModelAndView("com/gbiac/jeecgRPT/employeeSalaryDetail");
	}
	
	/**
	 * 根据公司、年月查询薪水册列表
	 * @param companyId 公司Id
	 * @param year 年份
	 * @param month 月份
	 */
	@RequestMapping(params = "getSalaryBook")
	@ResponseBody
	public AjaxJson getSalaryBook(Integer companyId,Integer year,Integer month,HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		List<Map<String,Object>> bookList =  DynamicDBUtil.findList(DBKEY_employeeSalary, salaryBookSql,companyId,year,month);
		j.setSuccess(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bookList",bookList);
		j.setAttributes(map);
		return j;
	}
	/**
	 * 白云国际机场日生产数据统计表
	 * 
	 * @return
	 */
	@RequestMapping(params = "dayAirport")
	public ModelAndView dayAirport(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/dayAirport");
	}
	/**
	 * 月报_机场吞吐量完成情况统计表（单月）
	 * 
	 * @return
	 */
	@RequestMapping(params = "monthAirport")
	public ModelAndView monthAirport(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/monthAirport");
	}
	/**
	 * 月报_机场吞吐量完成情况统计表（累计）
	 * 
	 * @return
	 */
	@RequestMapping(params = "totalAirport")
	public ModelAndView totalAirport(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/totalAirport");
	}
	/**
	 * 白云国际机场生产数据统计快报 
	 * 
	 * @return
	 */
	@RequestMapping(params = "airportFastNewspaper")
	public ModelAndView airportFastNewspaper(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/airportFastNewspaper");
	}
	/**
	 * 日报_机场吞吐量统计表
	 * 
	 * @return
	 */
	@RequestMapping(params = "dayAirportNewspaper")
	public ModelAndView dayAirportNewspaper(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/dayAirportNewspaper");
	}

	/**
	 * 翼展统计表
	 * 
	 * @return
	 */
	@RequestMapping(params = "ioCountByWings")
	public ModelAndView ioCountByWings(HttpServletRequest request) {
		return new ModelAndView("com/gbiac/jeecgRPT/ioCountByWings");
	}
	
}
